package com.guaju.yueshangLife.activity;

import android.Manifest;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.base.MyAppcomActivity;
import com.guaju.yueshangLife.databinding.ActivityRefundBinding;
import com.guaju.yueshangLife.imageloader.FrescoImgLoader;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;

import java.util.ArrayList;

import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;

public class RefundRequestActivity extends MyAppcomActivity {

    private ActivityRefundBinding binding;
    private static final int REQUEST_PERMISSION = 1001;
    private static final int REQUEST_SELECT_IMAGES_CODE1 = 1001;
    private static final int REQUEST_SELECT_IMAGES_CODE2 = 1002;
    private static final int REQUEST_SELECT_IMAGES_CODE3 = 1003;
    private static final int REQUEST_SELECT_REASON = 800;
    private static final int REQUEST_SELECT_TYPE = 801;
    public static final int RESULT_SELECT_REASON = 8000;
    public static final int RESULT_SELECT_TYPE = 8001;
    public static final String ORDER_ID="order_id";
    public static final String REQUEST_REASON = "reason";
    public static final String REQUEST_REASON_SELECTED = "reason_selected";
    public static final String REQUEST_TYPE = "type";
    public static final String REQUEST_TYPE_SELECTED = "type_selected";
    private int selectedType = -1;
    private int selectedReason = -1;
    private String[] typeArray = {"退货退款", "仅退款", "换货"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_refund);
        if (-1 != selectedType) {
            binding.tvType.setText(typeArray[selectedType]);
        }
        if (-1 != selectedReason) {
            binding.tvReason.setText(SelectReasonDialogActivity.reasons[selectedReason]);
        }
        setListener();
    }

    private void setListener() {
        binding.api1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                photo(REQUEST_SELECT_IMAGES_CODE1);
            }
        });
        binding.api2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                photo(REQUEST_SELECT_IMAGES_CODE2);
            }
        });
        binding.api3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                photo(REQUEST_SELECT_IMAGES_CODE3);
            }
        });
        binding.llRefundReason.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RefundRequestActivity.this, SelectReasonDialogActivity.class);
                intent.putExtra(REQUEST_REASON_SELECTED,selectedReason);
                startActivityForResult(intent, REQUEST_SELECT_REASON);
            }
        });
        binding.llRefundType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RefundRequestActivity.this, SelectRefundTypeActivity.class);
                intent.putExtra(REQUEST_TYPE_SELECTED,selectedType);
                startActivityForResult(intent, REQUEST_SELECT_TYPE);
            }
        });

    }

    private void photo(int requestCode) {

        PermissionGen.with(RefundRequestActivity.this)
                .addRequestCode(REQUEST_PERMISSION)
                .permissions(
                        Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .request();
        Intent intent = new Intent(this, ImageGridActivity.class);
        startActivityForResult(intent, requestCode);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SELECT_REASON) {
            if (data != null) {
                String reason = data.getStringExtra(REQUEST_REASON);
                selectedReason = data.getIntExtra(REQUEST_REASON_SELECTED, -1);
                if (!TextUtils.isEmpty(reason)) {
                    binding.tvReason.setText(reason);
                }
            } else {
                if (-1 == selectedReason) {
                    binding.tvReason.setText("请选择退款原因");
                }
            }
        } else if (requestCode == REQUEST_SELECT_TYPE) {
            if (data != null) {
                String reason = data.getStringExtra(REQUEST_TYPE);
                selectedType = data.getIntExtra(REQUEST_TYPE_SELECTED, -1);
                if (!TextUtils.isEmpty(reason)) {
                    binding.tvType.setText(reason);
                }
            } else {
                if (-1 == selectedType) {
                    binding.tvType.setText("请选择退款类型");
                }
            }

        }
        else if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (requestCode == REQUEST_SELECT_IMAGES_CODE1){
                if (data != null) {
                    ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                    ImageItem imageItem = images.get(0);
                    binding.api1.addImage(imageItem.path);
                }else {
                    Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
                }
            }else if (requestCode == REQUEST_SELECT_IMAGES_CODE2){
                if (data != null) {
                    ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                    ImageItem imageItem = images.get(0);
                    binding.api2.addImage(imageItem.path);
                }else {
                    Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
                }
            }else if (requestCode == REQUEST_SELECT_IMAGES_CODE3){
                if (data != null) {
                    ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                    ImageItem imageItem = images.get(0);
                    binding.api3.addImage(imageItem.path);
                }else {
                    Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
                }
            }

    }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @PermissionFail(requestCode = REQUEST_PERMISSION)
    public void doFailSomething() {
        Toast.makeText(this, "读取相册失败,为了您的用户体验，请您开启权限", Toast.LENGTH_SHORT).show();
    }

}
